package com.example.wangning.mycarofcountry.forum.famous;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.mycarofcountry.entity.dao.FamousList;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class FamousAdapter extends BaseAdapter {
    private List<FamousList> list;
    private Context context;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private int love_count;

    public FamousAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        volleySingleton = VolleySingleton.getVolleyIntance();
        imageLoader = volleySingleton.getImageLoader();
    }

    public void addData(List<FamousList> data) {
        list = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_duanzi_view, parent, false);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            final FamousList famouslist = list.get(position);
            myViewHolder.love.setVisibility(View.VISIBLE);
            myViewHolder.unloveTv.setVisibility(View.GONE);
            if (famouslist != null) {
                myViewHolder.text.setText(famouslist.getText());
                myViewHolder.name.setText(famouslist.getName());
                myViewHolder.created_at.setText(famouslist.getCreated_at());
                myViewHolder.love.setText(famouslist.getLove());
                myViewHolder.hate.setText(famouslist.getHate());
                myViewHolder.repost.setText(famouslist.getRepost());
                myViewHolder.comment.setText(famouslist.getComment());
                if (famouslist.getProfile_image() != null && famouslist.getProfile_image().length() > 0){
                    String url = famouslist.getProfile_image();
                    volleySingleton.getBitmap(volleySingleton,url,myViewHolder.profile_image);
                }
                if (famouslist.getLove() != null && famouslist.getLove().length() > 0){
                    String love = famouslist.getLove();
                    final boolean isLoveClick = false;
                    myViewHolder.love.setText(love);
                    myViewHolder.love.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            myViewHolder.isLoveClick = true;
                            love_count = Integer.parseInt(myViewHolder.love.getText().toString()) + 1;
                            famouslist.setLove(love_count + "");
                            if (myViewHolder.isLoveClick){
                                myViewHolder.love.setVisibility(View.GONE);
                                myViewHolder.unloveTv.setVisibility(View.VISIBLE);
                                myViewHolder.unloveTv.setText(love_count + "");
                            }
                        }
                    });
                    //判断是否点击
                   if (!myViewHolder.isLoveClick){
                       myViewHolder.love.setText(love);
                       myViewHolder.love.setVisibility(View.VISIBLE);
                       myViewHolder.unloveTv.setVisibility(View.GONE);
                   }
                    if (myViewHolder.isLoveClick){
                        myViewHolder.love.setVisibility(View.GONE);
                        myViewHolder.unloveTv.setVisibility(View.VISIBLE);
                        myViewHolder.unloveTv.setText(love_count + "");
                    }
                }else{
                    myViewHolder.love.setText(0);
                }
            }
        }

        return convertView;
    }

    class MyViewHolder {
        TextView text, name, created_at, love, hate,repost,comment,unloveTv;
        ImageView profile_image;
        private boolean isLoveClick;

        public MyViewHolder(View view) {
            text = (TextView) view.findViewById(R.id.textTv);
            name = (TextView) view.findViewById(R.id.nameTv);
            created_at = (TextView) view.findViewById(R.id.created_atTv);
            love = (TextView) view.findViewById(R.id.loveTv);
            hate = (TextView) view.findViewById(R.id.hateTv);
            profile_image = (ImageView) view.findViewById(R.id.profile_image);
            repost = (TextView) view.findViewById(R.id.repostTv);
            comment = (TextView) view.findViewById(R.id.commentTv);
            unloveTv = (TextView) view.findViewById(R.id.unloveTv);
            isLoveClick = false;
        }
    }
}
